UrlFlow and errorFlow are initialized with the state value and eagerly started#6688
UrlFlow and errorFlow are initialized with the state value and eagerly started#6688
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adjusts FrontendViewModel’s derived urlFlow and errorFlow so their .value reflects the current viewState even when there are no active subscribers, addressing a stateIn(WhileSubscribed) behavior where .value could remain null.
Changes:
- Switch
urlFlowanderrorFlowtostateIn(..., SharingStarted.Eagerly, initialValueFromState)instead ofWhileSubscribed. - Update unit tests to assert on
errorFlow.valuedirectly (without needing to collect first) and add a derived-flow test group.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| app/src/main/kotlin/io/homeassistant/companion/android/frontend/FrontendViewModel.kt | Makes derived flows eagerly started and initializes them from the current viewState to keep .value current |
| app/src/test/kotlin/io/homeassistant/companion/android/frontend/FrontendViewModelTest.kt | Updates tests around derived flows and removes reliance on “activate via subscription” for errorFlow |
I can understand this change and why it might be needed for race conditions, but not why you're referring to #6687 as that doesn't touch either of these two flows? |
I found the issue while working on the #6687 because my first implementation was using the |
Do you want to switch back to using the urlFlow after this is merged? |
No I don't think it is useful since we already have the UIState that is the source of truth, the main goal of urlFlow is for the error screen. |
Summary
From #6687, the
FrontendViewModel.urlFlowanderrorFloware always returning null if it doesn't have any subscriber and invoking.valueon it. It is because of thestateInWhileSubscribebehavior instead we should initialize with the state behavior and always collect to update this field. It is important since we are using.valuein few places so we need to make sure it reflect the current state (We could still have a race but regarding of where it is used I think we are fine).Checklist